package com.tieba.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.tieba.entity.TieBa;
import com.tieba.entity.User;

public class XMLBuilder {

	@Deprecated
	public static String XMLBuilderKey(String keyword) {
		String returnString = "";
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			Document doc = dbBuilder.parse("tieBa.xml");
			NodeList nList = doc.getElementsByTagName(keyword);
			for (int i = 0; i < nList.getLength(); i++) {
				Element node = (Element) nList.item(i);
				returnString = node.getFirstChild().getNodeValue();
			}
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnString;
	}

	@Deprecated
	public static List<String> readTieBaList() {
		List<String> returnList = new ArrayList<String>();
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			Document doc = dbBuilder.parse("tieBa.xml");
			NodeList nList = doc.getElementsByTagName("tieba");
			for (int i = 0; i < nList.getLength(); i++) {
				Element node = (Element) nList.item(i);
				returnList.add(node.getFirstChild().getNodeValue());
			}
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnList;

	}

	public static List<User> getUsersList() {
		List<User> usersList = new ArrayList<User>();
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(Constants.usersxml);

			NodeList list = doc.getElementsByTagName("user");
			for (int i = 0; i < list.getLength(); i++) {

				Element element = (Element) list.item(i);
				String id = element.getAttribute("id");
				String username = doc.getElementsByTagName("username").item(i).getFirstChild().getNodeValue();
				String password = doc.getElementsByTagName("password").item(i).getFirstChild().getNodeValue();
				double waittime = Double.parseDouble((doc.getElementsByTagName("waittime").item(i).getFirstChild().getNodeValue()));
				int autoupdate = Integer.parseInt(doc.getElementsByTagName("autoupdate").item(i).getFirstChild().getNodeValue());
				int retrycount = Integer.parseInt(doc.getElementsByTagName("retrycount").item(i).getFirstChild().getNodeValue());

				usersList.add(new User(id, username, password, autoupdate, waittime, retrycount));
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return usersList;
	}

	public static List<TieBa> getTiebaList() {
		List<TieBa> tieBaList = new ArrayList<TieBa>();
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(Constants.tiebaxml);

			NodeList list = doc.getElementsByTagName("tieba");
			for (int i = 0; i < list.getLength(); i++) {

				Element element = (Element) list.item(i);

				String id = element.getAttribute("id");
				String user = doc.getElementsByTagName("user").item(i).getFirstChild().getNodeValue();
				Long fid = Utils.Str2Long(doc.getElementsByTagName("fid").item(i).getFirstChild().getNodeValue());
				String kw = doc.getElementsByTagName("kw").item(i).getFirstChild().getNodeValue();
				String tname = doc.getElementsByTagName("tname").item(i).getFirstChild().getNodeValue();
				int order = Integer.parseInt(doc.getElementsByTagName("order").item(i).getFirstChild().getNodeValue());
				int falg = Integer.parseInt(doc.getElementsByTagName("falg").item(i).getFirstChild().getNodeValue());

				tieBaList.add(new TieBa(id, user, fid, kw, tname, order, falg));
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tieBaList;
	}

	public static void addTieBa(TieBa tieBa) {
		try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(Constants.tiebaxml);
			Element tiebaElement = doc.createElement("tieba");
			tiebaElement.setAttribute("id", tieBa.getId());

			Element userElement = doc.createElement("user");
			Element fidElement = doc.createElement("fid");
			Element tnameElement = doc.createElement("tname");
			Element orderElement = doc.createElement("order");
			Element falgElement = doc.createElement("falg");
			Element kwElement = doc.createElement("kw");

			userElement.appendChild(doc.createTextNode(tieBa.getUser()));
			fidElement.appendChild(doc.createTextNode(Utils.Long2Str(tieBa.getFid())));
			tnameElement.appendChild(doc.createTextNode(tieBa.getTname().toString()));
			orderElement.appendChild(doc.createTextNode(String.valueOf(tieBa.getOrder())));
			falgElement.appendChild(doc.createTextNode(String.valueOf(tieBa.getFalg())));
			kwElement.appendChild(doc.createTextNode(String.valueOf(tieBa.getKw())));

			tiebaElement.appendChild(userElement);
			tiebaElement.appendChild(fidElement);
			tiebaElement.appendChild(tnameElement);
			tiebaElement.appendChild(orderElement);
			tiebaElement.appendChild(falgElement);
			tiebaElement.appendChild(kwElement);

			Element tiebas = (Element) doc.getElementsByTagName("tiebalist").item(0);
			tiebas.appendChild(tiebaElement);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSourse = new DOMSource(doc);

			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream(Constants.tiebaxml));

			transformer.transform(domSourse, result);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} finally {

		}
	}

	public static void editTieBa(String id, TieBa tieBa) {
		try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(Constants.tiebaxml);
			Element newElement = doc.createElement("tieba");
			newElement.setAttribute("id", tieBa.getId());

			Element userElement = doc.createElement("user");
			Element fidElement = doc.createElement("fid");
			Element tnameElement = doc.createElement("tname");
			Element orderElement = doc.createElement("order");
			Element falgElement = doc.createElement("falg");

			userElement.appendChild(doc.createTextNode(tieBa.getUser()));
			fidElement.appendChild(doc.createTextNode(tieBa.getFid().toString()));
			tnameElement.appendChild(doc.createTextNode(tieBa.getTname().toString()));
			orderElement.appendChild(doc.createTextNode(String.valueOf(tieBa.getOrder())));
			falgElement.appendChild(doc.createTextNode(String.valueOf(tieBa.getFalg())));

			newElement.appendChild(userElement);
			newElement.appendChild(fidElement);
			newElement.appendChild(tnameElement);
			newElement.appendChild(orderElement);
			newElement.appendChild(falgElement);

			NodeList list = doc.getElementsByTagName("tieba");
			Element oldNlement = null;
			for (int i = 0; i < list.getLength(); i++) {
				Element element = (Element) list.item(i);
				if (element.getAttribute("id").equals(id)) {
					oldNlement = (Element) list.item(i);
				}
			}

			Element tiebas = (Element) doc.getElementsByTagName("tiebalist").item(0);
			tiebas.replaceChild(newElement, oldNlement);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSourse = new DOMSource(doc);

			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream(Constants.tiebaxml));

			transformer.transform(domSourse, result);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} finally {

		}
	}

	public static void delTieBa(String id) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(Constants.tiebaxml);

			NodeList list = doc.getElementsByTagName("tieba");
			Element delNlement = null;
			for (int i = 0; i < list.getLength(); i++) {
				Element element = (Element) list.item(i);
				if (element.getAttribute("id").equals(id)) {
					delNlement = (Element) list.item(i);
				}
			}

			Element tiebas = (Element) doc.getElementsByTagName("tiebalist").item(0);
			tiebas.removeChild(delNlement);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSourse = new DOMSource(doc);

			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream(Constants.tiebaxml));

			transformer.transform(domSourse, result);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
